package com.app.newiptv.service;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.WindowManager;

import com.app.newiptv.activity.HomePageActivity;
import com.app.newiptv.base.MyApplication;
//import com.app.newiptv.promote.SplashActivity;

import java.io.File;

/**
 * Created by dell on 2018/1/11.
 */

public class NetReceiver extends BroadcastReceiver {
    private final String TAG = "NetReceiver";
    private Context context;
    private AlertDialog dialog;

    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        boolean isNetWork = CheckNetWork();
        Log.i(TAG, "isNetWork = " + isNetWork);
        if (!isNetWork) {
            if(null!=dialog&&dialog.isShowing())
            {
                dialog.dismiss();
            }
//            AlertDialog.Builder confirm_dialog = new AlertDialog.Builder(context);
//            confirm_dialog.setMessage("检测网络状态中...")   // 设置显示的内容
//                    .setPositiveButton("取消", new DialogInterface.OnClickListener() {  // 添加确定按钮
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {       // 确定按钮的响应事件
//                            dialog.cancel();
//
//                        }
//                    })
//                    .setNegativeButton("设置查看", new DialogInterface.OnClickListener() {// 添加返回按钮
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {      // 响应事件
//                            //wirte the last system version when usb update system
//                            dialog.dismiss();
//                            PackageManager packageManager = context.getPackageManager();
//                            Intent intent = new Intent();
//                            intent = packageManager.getLaunchIntentForPackage("com.android.settings");
//                            context.startActivity(intent);
//
//                        }
//                    });
//            dialog = confirm_dialog.create();    // 在按键响应事件中显示此对话框
//
//            dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
//
//            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
//                @Override
//                public void onShow(DialogInterface dialogInterface) {
//                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();
//                    dialog.setOnShowListener(null);
//                }
//            });
//
//            dialog.show();
//            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();
//            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocusFromTouch();
        } else {
            Log.i(TAG, "dialog = " + (dialog == null ? "yes" : "no"));
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                dialog = null;
            }

            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("action_net_change"));

        }
    }

    public static boolean CheckNetWork() {
        ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.mApp
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null || !info.isAvailable()) {
            return false;
        } else {
            return true;
        }
    }
}
